<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>thread_pool</title>
    <link rel="stylesheet" href="../../boostbook.css" type="text/css">
    <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
    <link rel="home" href="../../index.html" title="Asio">
    <link rel="up" href="../reference.html" title="Reference">
    <link rel="prev" href="thread/_thread.html" title="thread::~thread">
    <link rel="next" href="thread_pool/add_service.html" title="thread_pool::add_service">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%">
    <tr>
        <td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../asio.png"></td>
    </tr>
</table>
<hr>
<div class="spirit-nav">
    <a accesskey="p" href="thread/_thread.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u"
                                                                                            href="../reference.html"><img
        src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png"
                                                                                      alt="Home"></a><a accesskey="n"
                                                                                                        href="thread_pool/add_service.html"><img
        src="../../next.png" alt="Next"></a>
</div>
<div class="section">
    <div class="titlepage">
        <div>
            <div><h3 class="title">
                <a name="asio.reference.thread_pool"></a><a class="link" href="thread_pool.html" title="thread_pool">thread_pool</a>
            </h3></div>
        </div>
    </div>
    <p>
        <a class="indexterm" name="asio.indexterm.thread_pool"></a>
    </p>
    <p>
        A simple fixed-size thread pool.
    </p>
    <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">thread_pool</span> <span
            class="special">:</span>
  <span class="keyword">public</span> <span class="identifier">execution_context</span>
</pre>
    <h5>
        <a name="asio.reference.thread_pool.h0"></a>
        <span><a name="asio.reference.thread_pool.types"></a></span><a class="link"
                                                                       href="thread_pool.html#asio.reference.thread_pool.types">Types</a>
    </h5>
    <div class="informaltable">
        <table class="table">
            <colgroup>
                <col>
                <col>
            </colgroup>
            <thead>
            <tr>
                <th>
                    <p>
                        Name
                    </p>
                </th>
                <th>
                    <p>
                        Description
                    </p>
                </th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool__basic_executor_type.html"
                           title="thread_pool::basic_executor_type"><span
                                class="bold"><strong>basic_executor_type</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Executor implementation type used to submit functions to a thread
                        pool.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/executor_type.html" title="thread_pool::executor_type"><span
                                class="bold"><strong>executor_type</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Executor used to submit functions to a thread pool.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/fork_event.html" title="thread_pool::fork_event"><span
                                class="bold"><strong>fork_event</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Fork-related event notifications.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/scheduler_type.html" title="thread_pool::scheduler_type"><span
                                class="bold"><strong>scheduler_type</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Scheduler used to schedule receivers on a thread pool.
                    </p>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <h5>
        <a name="asio.reference.thread_pool.h1"></a>
        <span><a name="asio.reference.thread_pool.member_functions"></a></span><a class="link"
                                                                                  href="thread_pool.html#asio.reference.thread_pool.member_functions">Member
        Functions</a>
    </h5>
    <div class="informaltable">
        <table class="table">
            <colgroup>
                <col>
                <col>
            </colgroup>
            <thead>
            <tr>
                <th>
                    <p>
                        Name
                    </p>
                </th>
                <th>
                    <p>
                        Description
                    </p>
                </th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/attach.html" title="thread_pool::attach"><span
                                class="bold"><strong>attach</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Attaches the current thread to the pool.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/executor.html" title="thread_pool::executor"><span
                                class="bold"><strong>executor</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Obtains the executor associated with the pool.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/get_executor.html" title="thread_pool::get_executor"><span
                                class="bold"><strong>get_executor</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Obtains the executor associated with the pool.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/join.html" title="thread_pool::join"><span
                                class="bold"><strong>join</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Joins the threads.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/notify_fork.html" title="thread_pool::notify_fork"><span
                                class="bold"><strong>notify_fork</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Notify the execution_context of a fork-related event.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/scheduler.html" title="thread_pool::scheduler"><span
                                class="bold"><strong>scheduler</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Obtains the scheduler associated with the pool.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/stop.html" title="thread_pool::stop"><span
                                class="bold"><strong>stop</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Stops the threads.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/thread_pool.html" title="thread_pool::thread_pool"><span
                                class="bold"><strong>thread_pool</strong></span></a> <span
                            class="silver">[constructor]</span>
                    </p>
                </td>
                <td>
                    <p>
                        Constructs a pool with an automatically determined number of threads.
                        <br> <span class="silver"> —</span><br> Constructs a pool with a specified number of threads.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/wait.html" title="thread_pool::wait"><span
                                class="bold"><strong>wait</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Waits for threads to complete.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/_thread_pool.html" title="thread_pool::~thread_pool"><span
                                class="bold"><strong>~thread_pool</strong></span></a> <span
                            class="silver">[destructor]</span>
                    </p>
                </td>
                <td>
                    <p>
                        Destructor.
                    </p>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <h5>
        <a name="asio.reference.thread_pool.h2"></a>
        <span><a name="asio.reference.thread_pool.protected_member_functions"></a></span><a class="link"
                                                                                            href="thread_pool.html#asio.reference.thread_pool.protected_member_functions">Protected
        Member Functions</a>
    </h5>
    <div class="informaltable">
        <table class="table">
            <colgroup>
                <col>
                <col>
            </colgroup>
            <thead>
            <tr>
                <th>
                    <p>
                        Name
                    </p>
                </th>
                <th>
                    <p>
                        Description
                    </p>
                </th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/destroy.html" title="thread_pool::destroy"><span class="bold"><strong>destroy</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Destroys all services in the context.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/shutdown.html" title="thread_pool::shutdown"><span
                                class="bold"><strong>shutdown</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Shuts down all services in the context.
                    </p>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <h5>
        <a name="asio.reference.thread_pool.h3"></a>
        <span><a name="asio.reference.thread_pool.friends"></a></span><a class="link"
                                                                         href="thread_pool.html#asio.reference.thread_pool.friends">Friends</a>
    </h5>
    <div class="informaltable">
        <table class="table">
            <colgroup>
                <col>
                <col>
            </colgroup>
            <thead>
            <tr>
                <th>
                    <p>
                        Name
                    </p>
                </th>
                <th>
                    <p>
                        Description
                    </p>
                </th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/add_service.html" title="thread_pool::add_service"><span
                                class="bold"><strong>add_service</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        (Deprecated: Use make_service().) Add a service object to the execution_context.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/has_service.html" title="thread_pool::has_service"><span
                                class="bold"><strong>has_service</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Determine if an execution_context contains a specified service
                        type.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/make_service.html" title="thread_pool::make_service"><span
                                class="bold"><strong>make_service</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Creates a service object and adds it to the execution_context.
                    </p>
                </td>
            </tr>
            <tr>
                <td>
                    <p>
                        <a class="link" href="thread_pool/use_service.html" title="thread_pool::use_service"><span
                                class="bold"><strong>use_service</strong></span></a>
                    </p>
                </td>
                <td>
                    <p>
                        Obtain the service object corresponding to the given type.
                    </p>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <p>
        The thread pool class is an execution context where functions are permitted
        to run on one of a fixed number of threads.
    </p>
    <h5>
        <a name="asio.reference.thread_pool.h4"></a>
        <span><a name="asio.reference.thread_pool.submitting_tasks_to_the_pool"></a></span><a class="link"
                                                                                              href="thread_pool.html#asio.reference.thread_pool.submitting_tasks_to_the_pool">Submitting
        tasks to the pool</a>
    </h5>
    <p>
        To submit functions to the thread pool, use the <a class="link" href="dispatch.html" title="dispatch"><code
            class="computeroutput"><span class="identifier">dispatch</span></code></a>, <a class="link" href="post.html"
                                                                                           title="post"><code
            class="computeroutput"><span class="identifier">post</span></code></a> or <a class="link" href="defer.html"
                                                                                         title="defer"><code
            class="computeroutput"><span class="identifier">defer</span></code></a> free functions.
    </p>
    <p>
        For example:
    </p>
    <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">my_task</span><span
            class="special">()</span>
<span class="special">{</span>
  <span class="special">...</span>
<span class="special">}</span>

<span class="special">...</span>

<span class="comment">// Launch the pool with four threads.</span>
<span class="identifier">asio</span><span class="special">::</span><span class="identifier">thread_pool</span> <span
                class="identifier">pool</span><span class="special">(</span><span class="number">4</span><span
                class="special">);</span>

<span class="comment">// Submit a function to the pool.</span>
<span class="identifier">asio</span><span class="special">::</span><span class="identifier">post</span><span
                class="special">(</span><span class="identifier">pool</span><span class="special">,</span> <span
                class="identifier">my_task</span><span class="special">);</span>

<span class="comment">// Submit a lambda object to the pool.</span>
<span class="identifier">asio</span><span class="special">::</span><span class="identifier">post</span><span
                class="special">(</span><span class="identifier">pool</span><span class="special">,</span>
    <span class="special">[]()</span>
    <span class="special">{</span>
      <span class="special">...</span>
    <span class="special">});</span>

<span class="comment">// Wait for all tasks in the pool to complete.</span>
<span class="identifier">pool</span><span class="special">.</span><span class="identifier">join</span><span
                class="special">();</span>
</pre>
    <h5>
        <a name="asio.reference.thread_pool.h5"></a>
        <span><a name="asio.reference.thread_pool.requirements"></a></span><a class="link"
                                                                              href="thread_pool.html#asio.reference.thread_pool.requirements">Requirements</a>
    </h5>
    <p>
        <span class="emphasis"><em>Header: </em></span><code class="literal">asio/thread_pool.hpp</code>
    </p>
    <p>
        <span class="emphasis"><em>Convenience header: </em></span><code class="literal">asio.hpp</code>
    </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%">
    <tr>
        <td align="left"></td>
        <td align="right">
            <div class="copyright-footer">Copyright © 2003-2022 Christopher M. Kohlhoff<p>
                Distributed under the Boost Software License, Version 1.0. (See accompanying
                file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
            </p>
            </div>
        </td>
    </tr>
</table>
<hr>
<div class="spirit-nav">
    <a accesskey="p" href="thread/_thread.html"><img src="../../prev.png" alt="Prev"></a><a accesskey="u"
                                                                                            href="../reference.html"><img
        src="../../up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../home.png"
                                                                                      alt="Home"></a><a accesskey="n"
                                                                                                        href="thread_pool/add_service.html"><img
        src="../../next.png" alt="Next"></a>
</div>
</body>
</html>
